package com.csdj.t301.controller;

import com.csdj.t301.aop.Role;
import com.csdj.t301.entity.Menus;
import com.csdj.t301.service.ICache;
import com.csdj.t301.service.MenuService;
import com.csdj.t301.vo.WebResult;
import org.apache.log4j.Logger;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


/**
 * author:cloud
 * date:2021/3/14 0014
 **/
@Controller
@RequestMapping("/sys")
public class SystemController {

    @Resource
    private MenuService menuService;
    @Resource
    private ICache redisService;
    private Logger log = Logger.getLogger(SystemController.class);

    @InitBinder
    public void initData(WebDataBinder dataBinder){
        System.out.println("自定义数据绑定在执行");
        dataBinder.registerCustomEditor(Date.class,
                new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),true));
    }

    @PostMapping("/upload")
    public String upload(@RequestParam("file") MultipartFile files, HttpServletRequest request) throws IOException {
        String filesName = files.getOriginalFilename();
        String path = request.getServletContext().getRealPath("/upload");
        File newFile = new File(path,filesName);
        InputStream is = null;
        OutputStream out = null;
        try {
            //检查硬盘上的文件是否存在
            if(!newFile.exists()){
                newFile.createNewFile();
            }
            //获取输入流
            is = files.getInputStream();
            //创建文件输出流
            out = new FileOutputStream(newFile);
            //jdk9的新方法， 零复制
            //is.transferTo(out);

            //老方法
            byte[] buf = new byte[1024];
            int read = 0;
            while( (read = is.read(buf))!=-1){
                out.write(buf,0,read);
            }

        }finally {
            if(is != null)is.close();
            if(out != null)out.close();
        }
        return "success";
    }


    /**
     * 测试数据绑定
     * @param date
     * @return
     */
    @GetMapping("/test/bind")
    @ResponseBody
    public WebResult testBind(Date date){
        System.out.println(date);
        return new WebResult(200);
    }


    /**
     * 给测试配置权限
     * @param request
     * @return
     */
    @GetMapping("/role/set")
    @ResponseBody
    public WebResult setRole(HttpServletRequest request){
        log.info("配置权限");
        log.error("配置权限");
        log.debug("配置权限");
        log.warn("配置权限");
        request.getSession().setAttribute("role","normal");
        return new WebResult(200);
    }

    @Role({"normal","admin"})
    @GetMapping("/role/test")
    @ResponseBody
    public WebResult testRole(){
        return new WebResult(200);
    }

    @GetMapping("/menu")
    @ResponseBody
    public WebResult allMenus(){
        WebResult result = new WebResult(200);
        log.info("中文测试");
        List<Menus> data = menuService.getAllMenus("admin");
        result.putData("data",data);
        return result;
    }

    @GetMapping("/redis")
    @ResponseBody
    public WebResult testRedis(){
        redisService.set("name","cloud");
        log.info(redisService.get("name"));
        redisService.sub();
        return new WebResult(200,"success");
    }

}
